
package com.tianyang.yukuaifu.common.utils;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianyang.yukuaifu.module.wheel.DateWheel;
import com.tianyang.yukuaifu.module.wheel.interfaces.IWheelDataChangeCallback;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Description : 日期选择处理
 */
public class DateCheckListener {

    private static final int INDEX_YEAR = 0;

    private static final int INDEX_MONTH = 1;

    private static final int INDEX_DAY = 2;

    private String DEFAULT_DATE;

    private final Activity context;

    public DateCheckListener(Activity context) {
        this.context = context;
        DEFAULT_DATE = "2017-01-01";
    }

    public DateCheckListener(Activity context, boolean currentTimeType) {
        this.context = context;
        if (currentTimeType) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            DEFAULT_DATE = format.format(date);
        } else {
            DEFAULT_DATE = "2017-01-01";
        }
    }

    /**
     * 处理滚轮选择事件
     *
     * @param parentView 父控件
     * @param showView   显示的控件
     */
    public void handleRegionCheck(LinearLayout parentView, TextView showView, boolean dayVisible) {
        handleRegionCheck(parentView, showView, dayVisible, null);
    }

    public void handleRegionCheck(LinearLayout parentView, TextView showView, boolean dayVisible,
                                  IWheelDataChangeCallback iWheelDataChangeCallback) {
        if (parentView == null) {
            return;
        }
        String showBirthday;
        if (showView.getText() != null && !TextUtils.isEmpty(showView.getText().toString())) {
            showBirthday = showView.getText().toString();
        } else {
            showBirthday = DEFAULT_DATE;
        }
        DateWheel dwView;
        if (!TextUtils.isEmpty(showBirthday) && showBirthday.contains("-")) {
            String[] split = showBirthday.split("-");
            if (dayVisible)
                dwView = new DateWheel(context, null, Integer.parseInt(split[INDEX_YEAR]),
                        Integer.parseInt(split[INDEX_MONTH]), Integer.parseInt(split[INDEX_DAY]));
            else
                dwView = new DateWheel(context, null, Integer.parseInt(split[INDEX_YEAR]),
                        Integer.parseInt(split[INDEX_MONTH]), 1);
        } else {
            dwView = new DateWheel(context);
        }
        MyClickListener regionClickListener = new MyClickListener(showView,
                iWheelDataChangeCallback);
        dwView.setWheelFinishListener(regionClickListener);
        dwView.setDayVisible(dayVisible);
        parentView.addView(dwView.getTimeView());
    }

    class MyClickListener implements DateWheel.OnWheelFinishListener {

        private final TextView showView;

        private final IWheelDataChangeCallback iWheelDataChangeCallback;

        public MyClickListener(TextView showView,
                               IWheelDataChangeCallback iWheelDataChangeCallback) {
            this.showView = showView;
            this.iWheelDataChangeCallback = iWheelDataChangeCallback;
        }

        @Override
        public void onChange(String currentValue) {
            if (!TextUtils.isEmpty(currentValue)) {
                if (iWheelDataChangeCallback != null) {
                    iWheelDataChangeCallback.wheelDataChangeCallback(currentValue);
                } else {
                    if (showView != null) {
                        showView.setText(currentValue);
                    }
                }
            }
        }
    }

}
